﻿using System;
using System.Collections.Generic;
using System.Linq;
using Soundteller.Bus.Handlers.Interfaces;
using Soundteller.Bus.Model.Interfaces;
using Soundteller.Bus.Repositories.Interfaces;

namespace Soundteller.Bus.Repositories
{
    public class MessageHandlerRepository : MemoryRepositoryBase, IMessageHandlerRepository
    {
        public MessageHandlerRepository(IMemoryContext memoryContext)
            : base(memoryContext)
        {
        }

        public List<IMessageHandler> GetByMessageType(IMessage message)
        {
            return (from handler in Context.MessageHandlers
                    where message.GetType() == handler.GetMessageType()
                    select handler).ToList();
        }

        public void Add(IMessageHandler messageHandler)
        {
            if (messageHandler == null) throw new ArgumentNullException("messageHandler");

            Context.MessageHandlers.Add(messageHandler);
        }

        public void Remove(IMessageHandler messageHandler)
        {
            if (messageHandler == null) throw new ArgumentNullException("messageHandler");

            Context.MessageHandlers.Remove(messageHandler);
        }

        public bool Exists(IMessageHandler messageHandler)
        {
            return Context.MessageHandlers.Contains(messageHandler);
        }
    }
}
